#ifndef CUSTOMERGEN_CPP
#define CUSTOMERGEN_CPP

#include "customerGen.h"

customerGen::customerGen(string fNameFile, string lNameFile, int toGen, int simStartTime, int simStopTime)
/*Preconditions:  Takes two strings that determine what the filenames for Customer Names will be.
                  Takes three integers: How many customers to generate, when the sim starts, and
                  when the sim ends.
  Postconditions: Seeds the customer generator for use later in the program.
*/
{
     int seed = time(0);	
     srand(time(0));
     if(simStartTime<0)
     {
          cout << "Error. Specified Start Time is less than 0." << endl;
          exit(0);
     }
     if(simStopTime>86400)
     {
          cout << "Error. Specified Stop Time past the end of a 24 hour day." << endl;
          exit(0);
     }
     if(simStartTime>simStopTime)
     {
          cout << "Error. Specified Start Time past specified Stop Time" << endl;
          exit(0);
     }
     ifstream nameStream1;
     ifstream nameStream2;
     nameStream1.open(fNameFile.c_str());
     if(!nameStream1)
     {
          nameStream1.close();
          cout << "Error. First Name File not found." << endl;
          exit(0);
     }
     nameStream2.open(lNameFile.c_str());
     if(!nameStream2)
     {
          nameStream1.close();
          nameStream2.close();
          cout << "Error. Last Name File not found." << endl;
          exit(0);
     }
     string buffer;
     while(!nameStream1.eof())
     {
         nameStream1 >> buffer;
         fNames.push_front(buffer);
     }
   
     while(!nameStream2.eof())
     {
         nameStream2 >> buffer;
         lNames.push_front(buffer);
     }
     
     for(int x = 0; x < toGen; x++)
     {
	  long int time = 0;
	  time = (long int)(1000000*d_uniform_01(&seed));
	  time = time%(simStopTime-simStartTime)+simStartTime;
	  custTimes.push_back(time);
     }
     
     custTimes.sort();
     nameStream1.close();
     nameStream2.close();
}


bool customerGen::next(customer &cust)
/*Preconditions:  Takes the address of a customer to store the generated information into.
  Postconditions: Returns true if the customer was successfully created.
*/
{
     if(custTimes.empty())
          return(false);

     list<string>::iterator current = NULL;
     int namepos = 0; 

     current = fNames.begin();
     namepos = rand()%fNames.size();
     for(int i = 0; i < namepos; i++){current++;}
     cust.fName = *current;

     current = lNames.begin();
     namepos = rand()%lNames.size();
     for(int i = 0; i < namepos; i++){current++;}
     cust.lName = *current;

     cust.arrive=custTimes.front();
     custTimes.pop_front();

     cust.items=(rand()%100)+1;
     
     if((rand()%1000)+1<=750)
     {
         cust.contest=true;
     }
     else
     {
         cust.contest=false;
     }
     if((rand()%1000)+1<=400)
     {
         cust.deli=true;
     }
     else
     {
         cust.deli=false;
     }
     //Defines payment method and line type
     
     int payRand=(rand()%1000)+1;
     if(payRand<=200)
     {
         cust.payType=CASH;
         if(cust.items<=10)
         {
              cust.lineType=EC;
         }
         else
         {
              cust.lineType=RC;
         }
     }
     else if(payRand>200&&payRand<=300)
     {
         cust.payType=CHECK;
         if(cust.items<=10)
         {
              cust.lineType=EL;
         }
         else
         {
              cust.lineType=RL;
         }
     }
     else
     {
         cust.payType=CARD;
         if(cust.items<=10)
         {
              cust.lineType=EL;
         }
         else
         {
              cust.lineType=RL;
         }
     }
     return(true);
}

int customerGen::nextTime()
/*Preconditions: None
  Postconditions: Returns the time of the next customer to be resolved, or
                  999999 (a sentinel) if there are no more customers to be
                  resolved.
*/
{
	if(!custTimes.empty())
		return(custTimes.front());
	else
		return 999999;
}

#endif
